package run.newbug.jt.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import run.newbug.jt.vo.ImageVO;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
@PropertySource("classpath:/images.properties")
public class FileServiceImpl implements FileService{
    @Value("${localDirPath}")
    private String localDirPath;
    @Value("${preURLPath}")
    private String preURLPath;

    @Override
    @Transactional
    public ImageVO upload(MultipartFile file) {
//        String filepath = "D:/images/";
//        File fileDir = new File(filepath);
//        if (!fileDir.exists()){
//            fileDir.mkdirs();
//        }
//        System.out.println(file);
//        String originalFilename = file.getOriginalFilename();
//        String path = filepath+originalFilename;
//        try {
//            file.transferTo(new File(path));
//        } catch (IOException e) {
//            e.printStackTrace();
//            throw new RuntimeException(e);
//        }

        String fileName = file.getOriginalFilename().toLowerCase();
        if(!fileName.matches("^.+\\.(jpg|png|gif|jpeg)$")){
            return null;
        }
        try {
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            if (height == 0 || width == 0){
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        String dateDirPath = new SimpleDateFormat("yyyy/MM/dd/").format(new Date());
        String localDir = localDirPath + dateDirPath;
        File fileDir = new File(localDir);
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }

        String uuid = UUID.randomUUID().toString().replace("-", "");
//        String fileType = fileName.split(".")
        int index = fileName.lastIndexOf(".");
        String fileType = fileName.substring(index);
        String realFileName = uuid+fileType;
        String realPath = localDir + realFileName;

        try {
            file.transferTo(new File(realPath));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        ImageVO imageVO = new ImageVO();
        imageVO.setVirtualPath(dateDirPath+realFileName)
                .setUrlPath(preURLPath+dateDirPath+realFileName)
                .setFileName(realFileName);
        System.out.println(imageVO);
        return imageVO;
    }

    @Override
    @Transactional
    public void deleteFile(String virtualPath) {
        String filePath = localDirPath+virtualPath;
        File file = new File(filePath);
        file.delete();
        return;
    }
}
